﻿<common:LayoutAwarePage
    x:Class="mPassRT.Pages.EntryPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:common="using:mPassRT.Common"
    xmlns:converters="using:mPassRT.Converters" xmlns:controls="using:mPassRT.Controls"
    mc:Ignorable="d">

    <Page.Resources>
        <Style x:Key="LabelStyle" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="14.667"/>
            <Setter Property="Margin" Value="0,0,0,5"/>
        </Style>
        
        <Style x:Key="FieldSeparation" TargetType="FrameworkElement">
            <Setter Property="Margin" Value="0,0,0,20"/>
        </Style>
        
        <Style x:Key="InlineButtonStyle" TargetType="Button">
            <Setter Property="MinWidth" Value="90"/>
        </Style>
        
        <converters:StringFormatConverter x:Key="FormatConverter"/>
        <converters:FallbackConverter x:Key="FallbackConverter"/>
        
        <x:String x:Key="PasswordLabelFormat">Password - {0} bit(s)</x:String>
    </Page.Resources>

    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{Binding Title, Converter={StaticResource FallbackConverter}, ConverterParameter={StaticResource NoEntryTitle}}" Style="{StaticResource PageHeaderTextStyle}"/>
            <TextBlock Text="{Binding Location}" Grid.Column="2" Style="{StaticResource PageSubheaderTextStyle}"/>
        </Grid>

        <Grid Grid.Row="1" Margin="116,20,40,46">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Row="0" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="Title"/>
                <TextBox Text="{Binding Title}"
                         controls:ImmediateSourceUpdate.IsEnabled="True"
                         controls:ImmediateSourceUpdate.Source="{Binding Title, Mode=TwoWay}"/>
            </StackPanel>

            <StackPanel Grid.Row="1" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="User Name"/>
                <TextBox Text="{Binding UserName}"
                         controls:ImmediateSourceUpdate.IsEnabled="true"
                         controls:ImmediateSourceUpdate.Source="{Binding UserName, Mode=TwoWay}"/>
            </StackPanel>

            <StackPanel Grid.Row="2" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="{Binding PasswordQuality, Converter={StaticResource FormatConverter}, ConverterParameter={StaticResource PasswordLabelFormat}}"/>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    
                    <PasswordBox Grid.Column="0" Password="{Binding Password, Mode=TwoWay}" IsPasswordRevealButtonEnabled="True"/>
                    <Button Grid.Column="1" Content="Copy" Style="{StaticResource InlineButtonStyle}" Command="{Binding CopyPasswordCommand}"/>
                    <Button Grid.Column="2" Content="Generate" Style="{StaticResource InlineButtonStyle}" Command="{Binding GeneratePasswordCommand}"/>
                </Grid>
            </StackPanel>

            <StackPanel Grid.Row="3" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="URL"/>
                <TextBox Text="{Binding Url}"
                         controls:ImmediateSourceUpdate.IsEnabled="true"
                         controls:ImmediateSourceUpdate.Source="{Binding Url, Mode=TwoWay}"/>
            </StackPanel>
            
            <StackPanel Grid.Row="4" Orientation="Horizontal" Style="{StaticResource FieldSeparation}">
                <Button Style="{StaticResource SaveAppBarButtonStyle}" Command="{Binding SaveCommand}"/>
                <Button Style="{StaticResource DiscardAppBarButtonStyle}" Command="{Binding DiscardCommand}"/>
            </StackPanel>

            <StackPanel Grid.Row="0" Grid.Column="2" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="Tags"/>
                <TextBox Text="{Binding Tags}"
                         controls:ImmediateSourceUpdate.IsEnabled="true"
                         controls:ImmediateSourceUpdate.Source="{Binding Tags, Mode=TwoWay}"/>
            </StackPanel>

            <StackPanel Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Style="{StaticResource FieldSeparation}">
                <TextBlock Style="{StaticResource LabelStyle}" Text="Notes"/>
                <TextBox Text="{Binding Notes}" Height="100" AcceptsReturn="True" 
                         TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto"
                         controls:ImmediateSourceUpdate.IsEnabled="true"
                         controls:ImmediateSourceUpdate.Source="{Binding Notes, Mode=TwoWay}"/>
            </StackPanel>
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>